/*
 * LogBookPanel.java
 *
 * Created on 23. červen 2008, 14:57
 */

package scubalogbook.gui.panels;
import java.awt.event.KeyEvent;
import java.lang.Object;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author  Administrator
 */
public class LogBookPanel extends javax.swing.JPanel {

    /** Creates new form LogBookPanel */
    public LogBookPanel() throws Exception {
        initComponents();
        
        LoadData();
    }

    private void DisplayDetail() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void EditDetail() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
   
    private void LoadData() throws Exception {                
//        DiveList diveList = scubalogbook.ScubaLogbookView.sqlite.GetAllDives();         
//        if (!diveList.equals(null))
//        {
//            Object[][] listOfDives = diveList.GetListOfDives();
//            String[] columnNames = diveList.GetColumnNames();
//            
//            LogbookTableModel logbookModel = new LogbookTableModel(columnNames, listOfDives);
//                                    
//            logBookTable.setModel(logbookModel);
//            
//            //Set column width
//            TableColumn column = null;
//            for (int i = 0; i < columnNames.length; i++) {
//                column = logBookTable.getColumnModel().getColumn(i);
//                if (i == 0) {
//                    column.setPreferredWidth(10);
//                    column.setWidth(10);
//                } else if (i == 1) {
//                    column.setPreferredWidth(50);
//                    column.setWidth(50);                            
//                } else if (i == 2) {
//                    column.setPreferredWidth(50);
//                    column.setWidth(50);                           
//                } else if (i == 3) {
//                    column.setPreferredWidth(50);
//                    column.setWidth(50);
//                } else if (i == 4) {
//                    column.setPreferredWidth(50);
//                    column.setWidth(50);
//                } else {
//                    column.setPreferredWidth(50);                    
//                }             
//            }     
//            logBookTable.repaint();
//            logBookTable.getSelectionModel().addListSelectionListener(
//                new ListSelectionListener() {
//                    public void valueChanged(ListSelectionEvent event) {
//                        int viewRow = logBookTable.getSelectedRow();
//                        if (viewRow > 0) {
//                            int modelRow = 
//                                logBookTable.convertRowIndexToModel(viewRow);
//                            DisplayDetail();
//                        }
//                    }
//                }
//        );
//
//        }
    }

    private void LoadDetail() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolBar = new javax.swing.JToolBar();
        newDiveButton = new javax.swing.JButton();
        deleteDiveButton = new javax.swing.JButton();
        downloadDivesButton = new javax.swing.JButton();
        hSplitButton = new javax.swing.JButton();
        vSplitButton = new javax.swing.JButton();
        splitPane = new javax.swing.JSplitPane();
        scrollPaneLeft = new javax.swing.JScrollPane();
        logBookTable = new javax.swing.JTable();
        scrollPaneRight = new javax.swing.JScrollPane();
        diveDetailPanel = new scubalogbook.gui.panels.DiveDetailPanel();

        setName("Form"); // NOI18N

        toolBar.setRollover(true);
        toolBar.setName("toolBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(scubalogbook.ScubaLogbookApp.class).getContext().getResourceMap(LogBookPanel.class);
        newDiveButton.setIcon(resourceMap.getIcon("newDiveButton.icon")); // NOI18N
        newDiveButton.setText(resourceMap.getString("newDiveButton.text")); // NOI18N
        newDiveButton.setToolTipText(resourceMap.getString("newDiveButton.toolTipText")); // NOI18N
        newDiveButton.setFocusable(false);
        newDiveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newDiveButton.setName("newDiveButton"); // NOI18N
        newDiveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(newDiveButton);

        deleteDiveButton.setIcon(resourceMap.getIcon("deleteDiveButton.icon")); // NOI18N
        deleteDiveButton.setText(resourceMap.getString("deleteDiveButton.text")); // NOI18N
        deleteDiveButton.setToolTipText(resourceMap.getString("deleteDiveButton.toolTipText")); // NOI18N
        deleteDiveButton.setFocusable(false);
        deleteDiveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteDiveButton.setName("deleteDiveButton"); // NOI18N
        deleteDiveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(deleteDiveButton);

        downloadDivesButton.setIcon(resourceMap.getIcon("downloadDivesButton.icon")); // NOI18N
        downloadDivesButton.setText(resourceMap.getString("downloadDivesButton.text")); // NOI18N
        downloadDivesButton.setToolTipText(resourceMap.getString("downloadDivesButton.toolTipText")); // NOI18N
        downloadDivesButton.setFocusable(false);
        downloadDivesButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        downloadDivesButton.setName("downloadDivesButton"); // NOI18N
        downloadDivesButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(downloadDivesButton);

        hSplitButton.setIcon(resourceMap.getIcon("hSplitButton.icon")); // NOI18N
        hSplitButton.setText(resourceMap.getString("hSplitButton.text")); // NOI18N
        hSplitButton.setToolTipText(resourceMap.getString("hSplitButton.toolTipText")); // NOI18N
        hSplitButton.setFocusable(false);
        hSplitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        hSplitButton.setName("hSplitButton"); // NOI18N
        hSplitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        hSplitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hSplitButtonActionPerformed(evt);
            }
        });
        toolBar.add(hSplitButton);

        vSplitButton.setIcon(resourceMap.getIcon("vSplitButton.icon")); // NOI18N
        vSplitButton.setText(resourceMap.getString("vSplitButton.text")); // NOI18N
        vSplitButton.setToolTipText(resourceMap.getString("vSplitButton.toolTipText")); // NOI18N
        vSplitButton.setFocusable(false);
        vSplitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        vSplitButton.setName("vSplitButton"); // NOI18N
        vSplitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        vSplitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vSplitButtonActionPerformed(evt);
            }
        });
        toolBar.add(vSplitButton);

        splitPane.setName("splitPane"); // NOI18N

        scrollPaneLeft.setName("scrollPaneLeft"); // NOI18N

        logBookTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        logBookTable.setName("logBookTable"); // NOI18N
        logBookTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                logBookTableMouseClicked(evt);
            }
        });
        logBookTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                logBookTableKeyPressed(evt);
            }
        });
        scrollPaneLeft.setViewportView(logBookTable);

        splitPane.setLeftComponent(scrollPaneLeft);

        scrollPaneRight.setName("scrollPaneRight"); // NOI18N

        diveDetailPanel.setName("diveDetailPanel"); // NOI18N

        javax.swing.GroupLayout diveDetailPanelLayout = new javax.swing.GroupLayout(diveDetailPanel);
        diveDetailPanel.setLayout(diveDetailPanelLayout);
        diveDetailPanelLayout.setHorizontalGroup(
            diveDetailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 661, Short.MAX_VALUE)
        );
        diveDetailPanelLayout.setVerticalGroup(
            diveDetailPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 482, Short.MAX_VALUE)
        );

        scrollPaneRight.setViewportView(diveDetailPanel);

        splitPane.setRightComponent(scrollPaneRight);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
            .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void hSplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hSplitButtonActionPerformed
    splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);    
}//GEN-LAST:event_hSplitButtonActionPerformed

private void vSplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vSplitButtonActionPerformed
    splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);    
}//GEN-LAST:event_vSplitButtonActionPerformed

private void logBookTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_logBookTableKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER)
    {
        EditDetail();
    }            
}//GEN-LAST:event_logBookTableKeyPressed

private void logBookTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logBookTableMouseClicked
    if (evt.getClickCount() == 2)
    {
        EditDetail();
    }
}//GEN-LAST:event_logBookTableMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteDiveButton;
    private scubalogbook.gui.panels.DiveDetailPanel diveDetailPanel;
    private javax.swing.JButton downloadDivesButton;
    private javax.swing.JButton hSplitButton;
    private javax.swing.JTable logBookTable;
    private javax.swing.JButton newDiveButton;
    private javax.swing.JScrollPane scrollPaneLeft;
    private javax.swing.JScrollPane scrollPaneRight;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JToolBar toolBar;
    private javax.swing.JButton vSplitButton;
    // End of variables declaration//GEN-END:variables

    



    class LogbookTableModel extends AbstractTableModel {
        private String[] columnNames = null;
        private Object[][] data = null;
                        
        public LogbookTableModel(String[] columnNames, Object[][] data)
        {
            this.columnNames = columnNames;
            this.data = data;                    
        }
        
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        @Override
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
//            if (col < 2) {
//                return false;
//            } else {
//                return true;
//            }
            return false;
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        @Override
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }        
    }
}
